from flask_wtf import FlaskForm
from flask_wtf.file import FileAllowed,FileField,FileRequired
from wtforms import SelectField, PasswordField, SubmitField, StringField, ValidationError, BooleanField, TextAreaField
from wtforms.validators import DataRequired, Length, EqualTo,Email
from app.models import User
from  app.extensions import photos

# 用户注册表
class RegisterForm(FlaskForm):
    username = StringField('用户名', validators={DataRequired(), Length(1,10,message='长度必须在6-10字符')},render_kw={'placeholder':'your account','style':'text-align: center'})
    password = PasswordField('密码',validators=[DataRequired(),Length(1,16,message='长度必须在6-16字符')],render_kw={'placeholder':'your password','style':'text-align: center'})
    confirm = PasswordField('确认密码',validators=[EqualTo('password',message='两次密码不一至')],render_kw={'placeholder':'your password agin','style':'text-align: center'})
    email = StringField('邮箱',validators=[Email(message='邮箱格式不正确')],render_kw={'placeholder':'example@163.com','style':'text-align: center'})
    submit = SubmitField('立即注册')

    # 自定义验证字段
    def validate_username(self,field):
        if User.query.filter_by(username=field.data).first():
            raise ValidationError('该用户已经存在！')

    def validate_email(self,field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('该邮箱已经存在！')

# 用户登录表单
class LoginForm(FlaskForm):
    username = StringField('',validators=[DataRequired(),Length(1,16,message='长度必须在6-16字符')],render_kw={'placeholder':'your account or email','style':'text-align: center'})
    password = PasswordField('',validators=[DataRequired(),Length(1,16,message='长度必须在6-16字符')],render_kw={'placeholder':'your password','style':'text-align: center'})
    remember = BooleanField('记住我')
    submit = SubmitField('登录')

# 修改密码
class ChangepawdForm(FlaskForm):
    password = PasswordField('请输入原密码',validators=[DataRequired(),Length(1,16,message='长度必须在6-16字符')])
    new_password = PasswordField('请输入修改后的密码', validators=[DataRequired(), Length(1, 16, message='长度必须在6-16字符')])
    verify_newpassword = PasswordField('请再次输入修改后的密码', validators=[EqualTo('new_password',message='两次密码不一至')])
    submit = SubmitField('确认修改')

# 上传表单
class ChangeiconForm(FlaskForm):
    icon =FileField('选择头像',validators=[FileRequired('请选择文件'),FileAllowed(photos,'文件格式不正确，只能上传图像')])
    submit = SubmitField('立即上传')

# 修改邮箱表单
class ChangeemailForm(FlaskForm):
    password = PasswordField('请确认用户密码',validators=[DataRequired(),Length(1,16,message='长度必须在6-16字符')])
    changeeamil = StringField('请输入修改后的邮箱',validators=[Email(message='邮箱格式不正确')],render_kw={'placeholder':'example@163.com','style':'text-align: center'})
    submit = SubmitField('确认修改')

# 找回密码表单
class FindpawdForm(FlaskForm):
    username = StringField('请输入您的注册邮箱',validators=[Email(message='邮箱格式不正确')],render_kw={'placeholder':'example@163.com','style':'text-align: center'})
    submit = SubmitField('找回密码')
# 找回密码表单2
class FindpawdFormTwo(FlaskForm):
    new_password = PasswordField('请输入修改后的密码', validators=[DataRequired(), Length(1, 16, message='长度必须在6-16字符')])
    verify_newpassword = PasswordField('请再次输入修改后的密码', validators=[EqualTo('new_password',message='两次密码不一至')])
    submit = SubmitField('确认重置')


